<?php
/*
 /*          Document   : JO_ringbacktoneController
             Created on : Sep 1, 2011, 10:43:03 PM
             Author     : Khuong Truong
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class ringbacktoneModule{
    public static function getArtist($_artist_key){
        $SQLCommand="select * from artist where md5(`key`)=$_artist_key";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null){
            $dt=$data->fetch_array();
        }else        return null;
        return $dt;
    }
    public static function getRandomArtist($_num){
        $SQLCommand="call getRandomArtist($_num)";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getRandomAlbum($_num){
        $SQLCommand="call getRandomAlbum($_num)";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getArtistFromRecomended($_recommended_key){
        $SQLCommand="call F_getArtistFromRecommended($_recommended_key)";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getAlbumFromRecomended($_recommended_key){
        $SQLCommand="call F_getAlbumFromRecommended('$_recommended_key')";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getAlbumsofArtist_($_artist_key,$_p,$_n){
        $SQLCommand="select album.*,artist.`name` from album,artist where album.`delete` is null and album.artist=artist.`key` and md5(album.artist)='$_artist_key' limit ".$_p*$_n.",$_n";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getAlbums($_p,$_n){
        $SQLCommand="select album.*,artist.`name` from album,artist where album.`delete` is null and album.artist=artist.`key` order by album.`subject` limit ".$_p*$_n.",$_n";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getArtists($_p,$_n){
        $SQLCommand="select artist.* from artist where artist.`delete` is null order by artist.`name` limit ".$_p*$_n.",$_n";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getAlbumofArtist($_album_key){
        $SQLCommand="select album.*,artist.`name` from album,artist where album.`delete` is null and album.artist=artist.`key` and md5(album.`key`)='$_album_key'";
        $data=DataProvider::executeSQL($SQLCommand);
        
        if($data!=null){
            $row=$data->fetch_array();
            return $row;
        }
        return null;
    }
    public static function getAlbumofArtist_numpage($_artist_key,$_n){
        $SQLCommand="select count(*) as numrow from album where album.`delete` is null and md5(album.artist)='$_artist_key'";
        $data=DataProvider::executeSQL($SQLCommand);
        if($data!=null) {
            $row=$data->fetch_array();
            return ($row["numrow"]%$_n==0)?(int)($row["numrow"]/$_n):((int)($row["numrow"]/$_n)+1);
        }
        return 0;
    }
     public static function getAlbums_numpage($_n){
        $SQLCommand="select count(*) as numrow from album where album.`delete` is null";
        $data=DataProvider::executeSQL($SQLCommand);
        if($data!=null) {
            $row=$data->fetch_array();
            return ($row["numrow"]%$_n==0)?(int)($row["numrow"]/$_n):((int)($row["numrow"]/$_n)+1);
        }
        return 0;
    }
    public static function getArtists_numpage($_n){
        $SQLCommand="select count(*) as numrow from artist where artist.`delete` is null";
        $data=DataProvider::executeSQL($SQLCommand);
        if($data!=null) {
            $row=$data->fetch_array();
            return ($row["numrow"]%$_n==0)?(int)($row["numrow"]/$_n):((int)($row["numrow"]/$_n)+1);
        }
        return 0;
    }

//    public static function getAlbumofArtist($_artist_key){
//        $SQLCommand="select * from album where `album`.`delete` is null and md5(album.artist)=$_artist_key";
//        $data=DataProvider::executeSQL($SQLCommand);
//        $dt=array();
//        if($data!=null) while ($row=$data->fetch_array()){
//                array_push($dt, $row);
//        }else        return null;
//        return $dt;
//    }
    public static function getRBTsofAlbum($_album_key,$_telco,$_p,$_num){
        $n=$_num;
        switch ($_telco){
            case "VTL8x88":
                $SQLCommand="
                    SELECT rbt_viettel.* FROM rbt_viettel,music,album
                    where music.`delete` is null
                    and album.delete is null and music.album=album.`key`
                    and md5(album.`key`)='$_album_key'
                    and viettel is not null
                    and music.`key`=rbt_viettel.music
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "VMS8x88":
                $SQLCommand="
                    SELECT rbt_mobifone.* FROM rbt_mobifone,music,album
                    where music.`delete` is null
                    and album.delete is null and music.album=album.`key`
                    and md5(album.`key`)='$_album_key'
                    and mobifone is not null
                    and music.`key`=rbt_mobifone.music
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "GPC8x88":
                $SQLCommand="
                    SELECT rbt_vinaphone.* FROM rbt_vinaphone,music,album
                    where music.`delete` is null
                    and album.delete is null and music.album=album.`key`
                    and md5(album.`key`)='$_album_key'
                    and vinaphone is not null
                    and music.`key`=rbt_vinaphone.music
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "VNM8x88":
                $SQLCommand="
                    SELECT rbt_vietnammobi.* FROM rbt_vietnammobi,music,album
                    where music.`delete` is null
                    and album.delete is null and music.album=album.`key`
                    and md5(album.`key`)='$_album_key'
                    and vietnammobi is not null
                    and music.`key`=rbt_vietnammobi.music
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "SFN8x88":
                $SQLCommand="
                    SELECT rbt_sfone.* FROM rbt_sfone,music,album
                    where music.`delete` is null
                    and album.delete is null and music.album=album.`key`
                    and md5(album.`key`)='$_album_key'
                    and sfone is not null
                    and music.`key`=rbt_sfone.music
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "BEE8x88":
                $SQLCommand="
                    SELECT rbt_beeline.* FROM rbt_beeline,music,album
                    where music.`delete` is null
                    and album.delete is null and music.album=album.`key`
                    and md5(album.`key`)='$_album_key'
                    and beeline is not null
                    and music.`key`=rbt_beeline.music
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
        }
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getRBTsofArtist($_artist_key,$_telco,$_p,$_num){
        $n=$_num;
        switch ($_telco){
            case "VTL8x88":
                $SQLCommand="
                    SELECT * FROM rbt_viettel
                    WHERE rbt_viettel.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and viettel is not null
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "VMS8x88":
                $SQLCommand="
                    SELECT * FROM rbt_mobifone
                    WHERE rbt_mobifone.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and mobifone is not null
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "GPC8x88":
                $SQLCommand="
                    SELECT * FROM rbt_vinaphone
                    WHERE rbt_vinaphone.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and vinaphone is not null
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "VNM8x88":
                $SQLCommand="
                    SELECT * FROM rbt_vietnammobi
                    WHERE rbt_vietnammobi.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and vietnammobi is not null
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "SFN8x88":
                $SQLCommand="
                    SELECT * FROM rbt_sfone
                    WHERE rbt_sfone.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and sfone is not null
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
            case "BEE8x88":
                $SQLCommand="
                    SELECT * FROM rbt_beeline
                    WHERE rbt_beeline.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and beeline is not null
                    order by note limit ".(($_p-1)*$n).",$n
                    ";
                break;
        }
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getRBTs($_telco,$_p,$_num){
        $telconame="";
        switch ($_telco){
            case "VTL8x88":
                $telconame="viettel";
                break;
            case "VMS8x88":
                $telconame="mobifone";
                break;
            case "GPC8x88":
                $telconame="vinaphone";
                break;
            case "VNM8x88":
                $telconame="vietnammobi";
                break;
            case "SFN8x88":
                $telconame="sfone";
                break;
            case "BEE8x88":
                $telconame="beeline";
                break;
        }
        $SQLCommand="
                    SELECT * FROM rbt_$telconame
                    WHERE $telconame is not null
                    order by note
                    limit ".(($_p-1)*$_num).",$_num
                    ";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getRBTs_num($_telco,$_num){
        $telconame="";
        switch ($_telco){
            case "VTL8x88":
                $telconame="viettel";
                break;
            case "VMS8x88":
                $telconame="mobifone";
                break;
            case "GPC8x88":
                $telconame="vinaphone";
                break;
            case "VNM8x88":
                $telconame="vietnammobi";
                break;
            case "SFN8x88":
                $telconame="sfone";
                break;
            case "BEE8x88":
                $telconame="beeline";
                break;
        }
        $SQLCommand="
                    SELECT count(*) as numrow FROM rbt_$telconame
                    WHERE $telconame is not null
                    ";
        $data=DataProvider::executeSQL($SQLCommand);
        if($data!=null){
            $row=$data->fetch_array();
            return ($row["numrow"]%$_num==0)?(int)($row["numrow"]/$_num):((int)($row["numrow"]/$_num)+1);
        }
        return 0;

    }
    public static function getRBTofArtist_pages($_artist_key,$_telco,$_num){
        $n=$_num;
        switch ($_telco){
            case "VTL8x88":
                $SQLCommand="
                    SELECT count(*) as numrow FROM rbt_viettel
                    WHERE rbt_viettel.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and viettel is not null
                    ";
                break;
            case "VMS8x88":
                $SQLCommand="
                    SELECT count(*) as numrow FROM rbt_mobifone
                    WHERE rbt_mobifone.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and mobifone is not null
                    ";
                break;
            case "VNM8x88":
                $SQLCommand="
                    SELECT count(*) as numrow FROM rbt_vietnammobi
                    WHERE rbt_vietnammobi.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and vietnammobi is not null
                    ";
                break;
            case "GPC8x88":
                $SQLCommand="
                    SELECT count(*) as numrow FROM rbt_vinaphone
                    WHERE rbt_vinaphone.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and vinaphone is not null
                    ";
                break;
            case "SFN8x88":
                $SQLCommand="
                    SELECT count(*) as numrow FROM rbt_sfone
                    WHERE rbt_sfone.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and sfone is not null
                    ";
                break;
            case "BEE8x88":
                $SQLCommand="
                    SELECT count(*) as numrow FROM rbt_beeline
                    WHERE rbt_beeline.`artistFK`=(
                        SELECT `name` from artist where `delete` is null and md5(`key`)=".$_artist_key."
                    ) and beeline is not null
                    ";
                break;
        }
        $data=DataProvider::executeSQL($SQLCommand);
        if($data!=null){
            $row=$data->fetch_array();
            return ($row["numrow"]%$n==0)?(int)($row["numrow"]/$n):((int)($row["numrow"]/$n)+1);
        }
        return 0;
        
    }
}
//======================end module============================
if(isset($_REQUEST["JO_action"])){
     switch ($_REQUEST["JO_action"]){
        case "show_rbt_artist":
            $artist_key=isset($_REQUEST["artist"])?$_REQUEST["artist"]:null;
            $telco_key=isset($_REQUEST["telco"])?$_REQUEST["telco"]:"VTL8x88";
            $rbtpage=1;
            include '../JO_004_RBTOBJ.php';
            break;
         case "show_album_artist":
            $artist_key=isset($_REQUEST["artist"])?$_REQUEST["artist"]:null;
            $albumpage=1;
            include '../JO_002_albumOBJ.php';
            break;
        case "show_random_artist":
            include '../JO_004_randomArtistOBJ.php';
            break;
        case "show_random_album":
            include '../JO_004_randomAlbumOBJ.php';
            break;
        case "show_artists":
            include '../JO_001_artistOBJ.php';
            break;
     }
}
?>
